import { Base64 } from "../../../utils/base64";
import { getTagData, isMobile, randomStr, randonNum } from "../../../utils/util"

const rules = [{
    name: 'loginPwd',
    rules: {required: true, message: '请输入新密码', rangelength: [6,20]},
},{
  name: 'loginName',
  rules: {required: true, message: '请输入手机号'},
},
{
  name: 'captcha',
  rules: {required: true, message: '请输入6位验证码', rangelength: [6,6]},
}
]



// pages/mine/pwd/pwd.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
      sendLoading: 0,
        formData: {},
        rules
    },

    formInputChange: function(e) {
        const {field} = getTagData(e);
        this.setData({
            [`formData.${field}`]: e.detail.value
        })
    },
    tapEyes: function() {
      this.setData({showPwd: !this.data.showPwd});
  },
    send: function (params) {
        const email = this.data.formData.email;
        if (!this.disableSend && email) {
            this.disableSend = true;
            const uuid = randomStr();
            this.setData({uuid})
            if (/^[^\@]+\@[^\@]+$/.test(email)) {
                wx.$http.get({url: "/szm/email", data: {uuid, email}, noToken: true}).then( res => {
                    this.setData({userId: res.data});
                    this.disableSend = false;
                    clearTimeout(this.timeoutId);
                    wx.showToast({
                        title: '邮件已发送',
                        icon: 'none'
                      })
                }).catch(err => {
                    this.disableSend = false;
                    clearTimeout(this.timeoutId);
                })

            } else {
                wx.showToast({
                  title: '请输入正确的邮箱',
                  icon: 'none'
                })
            }
            this.timeoutId = setTimeout(() => {
                this.disableSend = false;
            }, 20000);
        } else {
            wx.showToast({
              title: email ? '正在发送邮件' : '请输入邮箱地址',
              icon: 'none'
            })
        }
    },

    submitForm() {
        this.selectComponent('#form').validate((valid, errors) => {
            if (!valid) {
                const firstError = Object.keys(errors)
                if (firstError.length) {
                    wx.showToast({
                      title: errors[firstError[0]].message,
                      icon: 'none'
                    })
                }
            } else {
              const payload = {...this.data.formData};
              const arrayPwd = Array.from(randonNum(3) + payload.loginPwd, char => char.charCodeAt(0));
              if(arrayPwd.some(a => a > 0xFF)) {
                wx.showToast({
                  title: '不支持特殊密码',//123122@ MTIzMTIyQA==
                  icon: 'none'
                })
                return;
              }
              payload.loginPwd = Base64.base64EncArr(arrayPwd);
                wx.$http.put({url: '/szm/resetPwd', data: payload, noToken: true}).then(res => {
                  wx.showToast({
                    title: '重置密码',
                  })
                  setTimeout(()=>wx.navigateBack(), 2000)
                }).catch(err => {
                    console.log(err)
                })
              }
        })
    },

    tapSendSms() {
      this.data.formData.uuid = randomStr();
      if(this.data.sendLoading > 0) {
        return;
      }
      const {loginName, uuid} = this.data.formData;
      if(!isMobile(loginName)) {
        wx.showToast({
          title: '请输入手机号',
          icon:'none'
        })
        return;
      }
      this.setData({sendLoading: 60}, () => this.sendLoadingCount())
      wx.$http
        .post({url: "/szm/captcha.sms", data: { mobile: loginName, uuid: uuid, type: 3 }, noToken: true})
        .then(() => {
          wx.showToast({
            title: '发送短信',
          })
        })
        .catch(() => this.setData({sendLoading: 0}))
    },
  
    sendLoadingCount() {
      this.intervalId = setInterval(() => {
        let {sendLoading} = this.data;
        if (sendLoading > 0) {
          --sendLoading;
          this.setData({sendLoading})
        } else {
          clearInterval(this.intervalId);
        }
      }, 1000);
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
       
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})